package com.tanhua.dashboard.controller;



import com.tanhua.dashboard.service.DashBoardService2;
import com.tanhua.dashboard.vo.DistributionVo;
import com.tanhua.dashboard.vo.InfoStatisticsVo;
import com.tanhua.dashboard.vo.UsersStatisticsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("management/dashboard")
public class DashBoardController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DashBoardController.class);

    @Autowired
    private DashBoardService2 dashBoardService;

    @GetMapping("summary")
    public ResponseEntity<InfoStatisticsVo> InfoStatistics(){
        try {
            //todo
            InfoStatisticsVo infoStatistics = dashBoardService.InfoStatistics();
            if (null != infoStatistics) {
                return ResponseEntity.ok(infoStatistics);
            }
        } catch (NumberFormatException e) {
            LOGGER.error("查询统计数据失败！", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("users")
    public ResponseEntity<UsersStatisticsVo> getUsers(@RequestParam(value = "sd") Long sd,
                                                      @RequestParam(value = "ed") Long ed,
                                                      @RequestParam(value = "type") Integer type){
        try {
            UsersStatisticsVo usersStatisticsVo = dashBoardService.getUsers(sd,ed,type);
            if (null != usersStatisticsVo) {
                return ResponseEntity.ok(usersStatisticsVo);
            }
        } catch (NumberFormatException e) {
            LOGGER.error("查询统计数据失败！", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("distribution")
    public ResponseEntity<DistributionVo> getUsers(@RequestParam(value = "sd") Long sd,
                                                   @RequestParam(value = "ed") Long ed){
        try {
            DistributionVo distributionVo = dashBoardService.getDistribution(sd,ed);
            if (null != distributionVo) {
                return ResponseEntity.ok(distributionVo);
            }
        } catch (NumberFormatException e) {
            LOGGER.error("查询分布数据失败！", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
